<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2017/12/28
 * Time: 19:38
 */

namespace App\Http\Logics\Admin;

use App\Cafe;
use App\Partner;
use App\UserMappingRole;
use App\Users;
use App\Http\Logics\BaseLogic;
class PartnerLogic extends BaseLogic
{
    /**
     * 通过partner_id获取合作者的微信小程序配置
     * @param $partner_id 合作者ID
     * @return mixed 返回合作者信息
     */
    public static function getAppidByPartnerId($partner_id)
    {
        try{
            $partner = Partner::where('id', $partner_id)->first();
            return self::response(self::SUCCESS, $partner);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    /**
     * 获取合作者详情
     * @param $data 筛选信息
     * @return mixed 返回结果
     */
    public static function getPartnerDetail($data)
    {
        try{
            $response = self::getPartnerIdByLoginSession($data['loginSession']);
            if($response->code){
                return self::response(self::CONTENT_IS_EMPTY);
            }
            $partner_id = $response->data;

            $partner = Partner::select('id', 'name')->find($partner_id);
            if (empty($partner)){
                return self::response(self::CONTENT_IS_EMPTY);
            }

            $cafe = Cafe::where('partner_id', $partner_id)->first();
            if (empty($cafe)){
                return self::response(self::CONTENT_IS_EMPTY);
            }

            $partner->mobile = $cafe->mobile;
            $partner->address = $cafe->address;

            return self::response(self::SUCCESS, $partner);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    public static function getPartnerList($data)
    {
        try{
            $query = Partner::where(function ($query) use ($data){
                if (isset($data['type']) && $data['type']){
                    $query->where('type', $data['type']);
                }
                $query->where('id', '!=', 1);
            })->select('id', 'name', 'summary', 'sort', 'logo', 'weixin_name', 'type')->orderBy('id', 'desc');
            $partner = $data['limit'] ? $query->paginate($data['limit']) : $query->get();
            $partner->map(function ($partner) {
                return $partner->append('type_text');
            });
            if (empty($partner)){
                return self::response(self::CONTENT_IS_EMPTY);
            }
            return self::response(self::SUCCESS, $partner);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    public static function getPartnerDetailById($data)
    {
        $partner = Partner::select('id', 'name', 'summary', 'sort', 'logo', 'weixin_name')->find($data['partner_id']);
        if (empty($partner)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $partner);
    }

    public static function uploadLogo($data)
    {
        try{
            $url = uploadImage(storage_path().'/app/'.$data['logo_file'], 'city-banner');
            return self::response(self::SUCCESS, $url);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    public static function updatePartner($data)
    {
        $partner = Partner::find($data['partner_id']);
        if (empty($partner)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $partner->name = $data['name'];
        $partner->logo = $data['logo'];
        $partner->weixin_name = $data['weixin_name'];
        $partner->summary = $data['summary'];
        if (!$partner->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    /**
     * 添加合作者
     * @param $data
     * @return mixed
     */
    public static function createPartner($data)
    {
        $partner = new Partner();
        $partner->name = $data['name'];
        $partner->weixin_name = $data['weixin_name'];
        $partner->logo = $data['logo'];
        $partner->summary = $data['summary'];
        if (!$partner->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    public static function deletePartner($data)
    {
        $partner = Partner::find($data['partner_id']);
        if (empty($partner)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        if (!$partner->delete()){
            return self::response(self::OBJECT_DELETE_FAILD);
        }
        return self::response(self::SUCCESS);
    }
}